﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Skywi.Common.Entity;

namespace AudioCore.Common
{
    public class ServiceUtil
    {
        private const string ServiceListPath = @"E:\Projects\Enter\iMobile\Server\Common\ServiceList.xml";
        private static string SpecialChars = " ;-_\n";
        private static DataTable GetService()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(ServiceListPath);
            return ds.Tables[0];
        }

        public static bool CheckService(string serviceCode, out string errorMessage)
        {
            DataTable serviceList = GetService();
            //var row = from service in serviceList
            //          where service["ServiceCode"] == serviceCode
            //          select service;
            //if (row == null || row.Count() == 0)
            //{
            //    errorMessage = string.Format("Khong tim thay DV '{0}' trong he thong", serviceCode);
            //    return false;
            //}

            DataRow[] rows = serviceList.Select(string.Format("ServiceCode='{0}'", serviceCode));
            if (rows == null || rows.Count() == 0)
            {
                errorMessage = string.Format("Khong tim thay DV '{0}' trong he thong", serviceCode);
                return false;
            }
            errorMessage = string.Empty;
            return true;
        }
        /// <summary>
        /// Split a response content to many of SMSs
        /// </summary>
        /// <param name="responseContent"></param>
        /// <returns></returns>
        public static List<string> MakeMTContent(string responseContent)
        {
            List<string> result = new List<string>();
            string strTmp = responseContent;
            int smsLength = GetApprovedStringLength(strTmp, Constant.MAX_LENGHT_SMS);
            while (true)
            {
                if (smsLength < strTmp.Length)
                {
                    result.Add(strTmp.Substring(0, smsLength));
                    strTmp = strTmp.Remove(0, smsLength + 1);
                }
                else
                {
                    result.Add(strTmp);
                    break;
                }
                smsLength = GetApprovedStringLength(strTmp, Common.Constant.MAX_LENGHT_SMS);
            }
            return result;
        }
        /// <summary>
        /// Lay ve do dai cua string ma co the cat ra tu strTmp sao cho thoa man cac dieu kien:
        ///     + Do dai nho hon hoac bang maxLength
        ///     + Bao toan word cua original string (khong word nao bi cat ra).
        /// </summary>
        /// <param name="strTmp"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        private static int GetApprovedStringLength(string strTmp, int maxLength)
        {
            int result = 0;
            if (maxLength >= strTmp.Length)
            {
                result = strTmp.Length;
            }
            else
            {
                if (strTmp[maxLength] != ' ')
                {
                    result = GetLastSpacePos(strTmp, maxLength);
                }
                else
                {
                    result = maxLength;
                }
            }
            return result;
        }
        /// <summary>
        /// Get position (less than maxPos) of last special character in str
        /// </summary>
        /// <param name="str"></param>
        /// <param name="maxPos"></param>
        /// <returns></returns>
        private static int GetLastSpacePos(string str, int maxPos)
        {
            if (maxPos <= str.Length)
            {
                for (int i = maxPos; i >= 0; i--)
                {
                    if (SpecialChars.Contains(str[i]))
                        return i;
                }
            }
            return -1;
        }
    }
}
